#include "processor.h"
#include "handler.h"

/* INIT */

Processor::Processor(QObject *parent) : QObject(parent), _pkgBuff(40)
{
}

Processor::~Processor()
{
	delete _api;
	delete _pkgManager;
}

void						Processor::initConnect()
{
	connect(_api->gui(), SIGNAL(newConnexion(const connectInfo)), this, SLOT(handleConnect(const connectInfo)));
	connect(_api->network(), SIGNAL(connected(QHostAddress &)), this, SLOT(handleConnected(QHostAddress &)));
	connect(_api->network(), SIGNAL(disconnected(QHostAddress &)), this, SLOT(handleDisconnected(QHostAddress &)));
	connect(_api->gui(), SIGNAL(changeMode(quint16, bool)), this, SLOT(handleMode(quint16, bool)));
	connect(_api->gui(), SIGNAL(config(quint16)), this, SLOT(handleConfig(quint16)));
	connect(_api->audio(), SIGNAL(newPacket(Packet&)), this, SLOT(handleNewPacket(Packet&)));
	connect(_api->audio(), SIGNAL(newSound(SoundPacket*)), this, SLOT(handleNewSound(SoundPacket*)));
	connect(_api->client(), SIGNAL(recordMod(bool)), this, SLOT(handleRecordMod(bool)));
}

void						Processor::setApi(Api *api)
{
	_api = api;
	initConnect();
	_pkgManager = new PacketManager(*_api, this);

	///*TMP*/ _api->audio()->startRecord();
	//_api->timer()->start();
}

PacketHandler				*Processor::getPacketHandler()
{
	return new PacketHandler(*this);
}

void						Processor::interpret(Packet *data)
{
	_pkgManager->interpret(*data);
}

/* SLOTS */

void						Processor::handleConnect(const connectInfo info)
{
	quint16				id;
	clientInfo			clientInfo;
	QHostAddress		ip = info._ip;

	qDebug() << "Processor::handleConnect : IP = " << info._ip << " || Port = " << info._port;

	if (!_api->network()->isInside(ip))
	{
		id = _api->network()->connectTo(info._ip.toString().toStdString(), "4242");
		_api->client()->addClient( id );
		clientInfo = _api->client()->getClient(id);
		_api->gui()->addServer(id, clientInfo._name);
	}
}

void						Processor::handleConnected(QHostAddress &ip)
{
	quint16					id = _api->network()->getId(ip);
	Packet*					pkg = _pkgManager->buildPacket(MAIN, CONNECT);

	_api->network()->sendToTcp(*pkg, id);
	delete pkg;
}

void						Processor::handleDisconnected(QHostAddress &ip)
{
	quint16					id = _api->network()->getId(ip);

	qDebug() << ip << "is now disconnected";
	_api->network()->removeClient(id);
	_api->client()->removeClient(id);
	_api->gui()->removeServer(id);
}

void						Processor::handleMode(quint16 id, bool mode)
{
	_api->client()->setAvaible(mode, id);
}

void						Processor::handleConfig(quint16 id)
{
	QString						url = QString("http://") + _api->network()->getIp(id).toString() + QString(":8080");
	QUrl						link = QUrl(url);

	QDesktopServices::openUrl(link);
}

void						Processor::handleNewPacket(Packet& pkg)
{
	std::list<quint16>		avaibles = _api->client()->getAvaibles();

	if (!avaibles.empty()){
		for(std::list<quint16>::iterator it = avaibles.begin(); it != avaibles.end(); ++it){
			_api->network()->sendToUdp(pkg, (*it));
		}
	}
}

void						Processor::handleNewSound(SoundPacket *sound)
{
	Packet						*pkg = _pkgBuff.get();
	//Packet						*pkg = new Packet; pkg->init();
	ProtocolOperator			proto = AUDIO;
	ProtocolAudio				op = AUDIOPACKET;
	unsigned int				time = _api->timer()->getTime();

	pkg->pushData<ProtocolOperator>(&proto);
	pkg->pushData<ProtocolAudio>(&op);

	_api->audio()->getEncoder().encode(sound, pkg);
	sound->setTime(time);
	//delete sound;
	handleNewPacket(*pkg);
}

void						Processor::handleRecordMod(bool mod)
{
	if (mod)
		_api->audio()->startRecord();
	else
		_api->audio()->stopRecord();
}

/* !SLOTS */